﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Geometries.Utilities;

namespace NetTopologySuite.Precision
{
	[Obsolete("Use GeometryPrecisionReducer")]
	public class SimpleGeometryPrecisionReducer
	{
		public static IGeometry Reduce(IGeometry g, PrecisionModel precModel)
		{
			return new SimpleGeometryPrecisionReducer(precModel).Reduce(g);
		}

		public SimpleGeometryPrecisionReducer(PrecisionModel pm)
		{
			this._newPrecisionModel = pm;
		}

		public bool RemoveCollapsedComponents
		{
			get
			{
				return this._removeCollapsed;
			}
			set
			{
				this._removeCollapsed = value;
			}
		}

		public bool ChangePrecisionModel
		{
			get
			{
				return this._changePrecisionModel;
			}
			set
			{
				this._changePrecisionModel = value;
			}
		}

		public IGeometry Reduce(IGeometry geom)
		{
			GeometryEditor geometryEditor;
			if (this._changePrecisionModel)
			{
				geometryEditor = new GeometryEditor(new GeometryFactory(this._newPrecisionModel));
			}
			else
			{
				geometryEditor = new GeometryEditor();
			}
			return geometryEditor.Edit(geom, new SimpleGeometryPrecisionReducer.PrecisionReducerCoordinateOperation(this));
		}

		private readonly PrecisionModel _newPrecisionModel;

		private bool _removeCollapsed = true;

		private bool _changePrecisionModel;

		private class PrecisionReducerCoordinateOperation : GeometryEditor.CoordinateOperation
		{
			public PrecisionReducerCoordinateOperation(SimpleGeometryPrecisionReducer container)
			{
				this._container = container;
			}

			public override Coordinate[] Edit(Coordinate[] coordinates, IGeometry geom)
			{
				if (coordinates.Length == 0)
				{
					return null;
				}
				Coordinate[] array = new Coordinate[coordinates.Length];
				for (int i = 0; i < coordinates.Length; i++)
				{
					Coordinate coordinate = new Coordinate(coordinates[i]);
					this._container._newPrecisionModel.MakePrecise(coordinate);
					array[i] = coordinate;
				}
				Coordinate[] array2 = new CoordinateList(array, false).ToCoordinateArray();
				int num = 0;
				if (geom is ILineString)
				{
					num = 2;
				}
				if (geom is ILinearRing)
				{
					num = 4;
				}
				Coordinate[] result = array;
				if (this._container._removeCollapsed)
				{
					result = null;
				}
				if (array2.Length < num)
				{
					return result;
				}
				return array2;
			}

			private readonly SimpleGeometryPrecisionReducer _container;
		}
	}
}
